<?php

class Default_Model_Project extends Zend_Db_Table_Abstract
{
    protected $_name = 'project';
    public function getAllData(){
        $query = $this->select();
        $query->from($this->_name, array('projectId','projectName','note'));
        
        return $this->fetchAll($query);
    }
    public function getListData($project_list){
        $query = $this->select();
        $query->from($this->_name, array('projectId','projectName','note'));
        $query->where('projectId IN ('.implode(',' ,$project_list).')');
        $query->order('projectName ASC');
        
        return $this->fetchAll($query);
    }
    public function getDataById($id){
        $id = (int)($id);
        $query = $this->select();
        $query->from($this->_name, array('projectId','projectName','note'));
        $query->where('projectId = ?', $id);
        
        return $this->fetchRow($query);
    }
    public function getDataByEquipmentId($id){
        $id = (int)($id);
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p' => $this->_name), array('p.projectName'))
                        ->joinLeft(array('ep'=>'equipment_project'), 'p.projectId = ep.projectId')
                        ->where('ep.equipmentId = ?',$id)
                        ->group('p.projectName');
                        
        return $this->fetchAll($query);
    }
    public function getListDataByEquipmentId($id){
        $id = (int)($id);
        $query = "SELECT GROUP_CONCAT(p.projectName) as project_list FROM project p, equipment_project ep WHERE p.projectId = ep.projectId and ep.equipmentId = {$id} ";        
        
        return $this->fetchRow($query);
    }
    public function updateData($projectId, $projectName, $note)
    {
        $data = array ('projectName' => $projectName,'note' => $note);
        $this->update( $data, 'projectId = ' . (int) $projectId );
    }
    public function checkProject($projectId)
    {
        $projectId = (int)($projectId);
        $query = $this->select()
                        ->setIntegrityCheck(false)
                        ->from(array('p' => $this->_name), array('p.projectName'))
                        ->joinLeft(array('ep'=>'equipment_project'), 'p.projectId = ep.projectId',array())
                        ->where('ep.projectId = ?',$projectId)
                        ->group('p.projectName');
                        
        return $this->fetchAll($query);
    }
    public function deleteData($projectId)
    {
        $this->delete('projectId =' . (int)($projectId));
    }
    public function addData($projectName, $note, $createUser = 1)
    {
        $data = array ( 'projectName' => $projectName, 'note' => $note, 'createUser' => $createUser, 'createDate' => date('Y-m-d H:i:s'));
        return $this->insert( $data );
    }
    
    //chua su dung
    public function findForSelect ()
    {

        $select = $this->select();
        return $this->fetchAll( $select );
    }
    public function getItems($option = array())
    {
        $count = isset($option['count'])? $option['count'] : 0;
        $offset = isset($option['offset'])? $option['offset'] : 0;
        $order = isset($option['order'])? $option['order'] : 'projectName';
        $sort = isset($option['sort'])? $option['sort'] : 'asc';
        $select = $this->select()->from(array('s' => "project" ),
					 array('projectId',
						 'projectName',
						 'note'))
			 ->order("{$order} {$sort}")
			 ->limit($count,$offset);
        $results = $this->fetchAll($select);
        return $results;
    }

    private function getProjectId($userId)
    {
		$selectProject = 'Select p.projectId FROM privileges as p, users_roles as ur Where p.roleId = ur.roleId and ur.userId = ' . $userId;
        $selectProject = $this->getDefaultAdapter()->query( $selectProject );
		return $project = $selectProject->fetchAll(PDO::FETCH_ASSOC);
    }

}

























